# Maintainer: Carlos Antunes <cmantunes@gmail.com>
# Contributed by: nanodude

_realname=gcc
_target=arm-none-eabi

pkgbase=mingw-w64-${_target}-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=8.2.0
pkgrel=1
pkgdesc="GNU Tools for ARM Embedded Processors - GCC (mingw-w64)"
arch=('any')
url="https://launchpad.net/gcc-arm-embedded"
license=('GPL' 'Various')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-${_target}-newlib"
         "${MINGW_PACKAGE_PREFIX}-${_target}-binutils")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' '!debug' '!strip')
source=('gcc-arm-embedded.tar.bz2::https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q3-update/+download/gcc-arm-none-eabi-4_8-2014q3-20140805-src.tar.bz2'
	'010-gcc-isl-0.13.patch'
	'020-gcc-auto-host-screwup.patch')
sha256sums=('09519946642d5366ab739a89d3388b2ea333cd7dc50192bab3736a9593af83e6'
            '8e8da71e6e6b347dd7fc7f7938bf9af22be031ad8916749fa6c368f87d8891f0'
            'b5e826a0dc477f04233e68cc7d0dfc4f498c36e16c5cc3751cb52bcd9a7f0c24')
noextract=('gcc-arm-embedded.tar.bz2')

prepare() {
  # Extract packages from archive
  rm -rf ${srcdir}/tmp
  mkdir ${srcdir}/tmp && cd ${srcdir}/tmp
  tar xf ../../gcc-arm-embedded.tar.bz2 --strip-components=2

  # Copy and extract GCC package
  cp gcc.tar.bz2 ${srcdir}
  cd ${srcdir}
  [ -d gcc ] && rm -rf gcc
  tar xf gcc.tar.bz2

  # GCC: Compatibility with isl>=0.12.2
  cd "${srcdir}/gcc"
  patch -p0 -i "${srcdir}/010-gcc-isl-0.13.patch"

  # GCC: deal with auto-host.h hack in crtstuff.c
  patch -p1 -i "${srcdir}/020-gcc-auto-host-screwup.patch"
}

build() {
  # Build GCC
  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  unset CFLAGS
  unset CXXFLAGS
  CFLAGS="-g -O3 -fdata-sections -ffunction-sections"
  CXXFLAGS="${CFLAGS}"
  ../${_realname}/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${_target}" \
    --prefix="${MINGW_PREFIX}" \
    --enable-languages="c,c++" \
    --disable-werror \
    --disable-lto \
    --disable-nls \
    --disable-libffi \
    --disable-decimal-float \
    --disable-libgomp \
    --disable-libmudflap \
    --disable-libquadmath \
    --disable-libssp \
    --disable-libstdcxx-pch \
    --disable-libsanitizer \
    --disable-threads \
    --disable-tls \
    --disable-shared \
    --enable-multilib \
    --enable-interwork \
    --with-newlib \
    --with-headers=yes \
    --with-native-system-header-dir=/include \
    --with-gnu-as \
    --with-gnu-ld \
    --with-system-zlib \
    --with-python-dir="share/${_target}-${_realname}" \
    --with-libelf \
    --with-{gmp,mpfr,mpc,isl,cloog}="${MINGW_PREFIX}" \
    --disable-isl-version-check \
    --disable-cloog-version-check \
    --with-multilib-list="armv6-m,armv7-m,armv7e-m,armv7-r" \
    --with-host-libstdcxx="-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" \
    --with-sysroot="${MINGW_PREFIX}"
  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -j1 DESTDIR=${pkgdir} install

  # Remove files that conflict with host
  rm -rf "${pkgdir}${MINGW_PREFIX}/share/info"
}

